import type { DepartmentList, DepartmentsItem, TreeData } from '@/types/departments'

const filterTreeData = (list: Array<DepartmentsItem>, rootValue: string = '') => {
  const hasChild = list.some((el) => el.pid === rootValue)
  if (hasChild) {
    const arr: Array<DepartmentsItem> = []
    list.forEach((item) => {
      if (item.pid === rootValue) {
        const children = filterTreeData(list, item.id)
        item.children = children
        arr.push(item)
      }
    })
    return arr
  } else {
    return
  }
}

export function getTreeData(res: DepartmentList): TreeData {
  return [
    {
      id: '',
      name: res.companyName,
      companyId: res.companyId,
      manager: res.companyManage || '负责人',
      children: filterTreeData(res.depts)
    }
  ]
}
